home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 22 / CU Amiga Magazine's Super CD-ROM 22 (1998)(EMAP Images)(GB)[!][issue 1998-05].iso / PowerPC / Archivers / ZLib / adler32.c next >
Encoding:
C/C++ Source or Header  |  1998-02-20  |  2.1 KB  |  99 lines

  1. /* adler32.c -- compute the Adler-32 checksum of a data stream
  2.  * Copyright (C) 1995-1996 Mark Adler
  3.  * For conditions of distribution and use, see copyright notice in zlib.h 
  4.  */
  5.  
  6. /* $Id: adler32.c,v 1.10 1996/05/22 11:52:18 me Exp $ */
  7.  
  8. #include "zlib.h"
  9. #include <stdio.h>
  10. #include <dos/dos.h>
  11. #include <dos/dosextens.h>
  12. #include <errno.h>
  13. #include <filedefs.h>
  14. #include <clib/exec_protos.h>
  15. #include <time.h>
  16. #include <exec/types.h>
  17. #include <libraries/dos.h>
  18. #include <clib/dos_protos.h>
  19. #include <stdlib.h>
  20. #include <sys/dir.h>
  21.  
  22. #define BASE 65521L /* largest prime smaller than 65536 */
  23. #define NMAX 5552
  24. /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
  25.  
  26. #define DO1(buf,i)  {s1 += buf[i]; s2 += s1;}
  27. #define DO2(buf,i)  DO1(buf,i); DO1(buf,i+1);
  28. #define DO4(buf,i)  DO2(buf,i); DO2(buf,i+2);
  29. #define DO8(buf,i)  DO4(buf,i); DO4(buf,i+4);
  30. #define DO16(buf)   DO8(buf,0); DO8(buf,8);
  31.  
  32. /* ========================================================================= */
  33. uLong adler32(uLong adler, const Bytef *buf, uInt len)
  34. {
  35.     unsigned long s1 = adler & 0xffff;
  36.     unsigned long s2 = (adler >> 16) & 0xffff;
  37.     int k;
  38.  
  39.     if (buf == Z_NULL) return 1L;
  40.  
  41.     while (len > 0) {
  42.         k = len < NMAX ? len : NMAX;
  43.         len -= k;
  44.         while (k >= 16) {
  45.             DO16(buf);
  46.             buf += 16;
  47.             k -= 16;
  48.         }
  49.         if (k != 0) do {
  50.             s1 += *buf++;
  51.             s2 += s1;
  52.         } while (--k);
  53.         s1 %= BASE;
  54.         s2 %= BASE;
  55.     }
  56.     return (s2 << 16) | s1;
  57. }
  58.  
  59. #define MAX_FILES 32
  60.  
  61. static FILE* handles[MAX_FILES] ;
  62.  
  63. extern "C" void INIT_8_FileHandles(void)
  64. {
  65.     int i ;
  66.     handles[0] = stdin ;
  67.     handles[1] = stdout ;
  68.     handles[2] = stderr ;
  69.  
  70.     for (i=3; i < MAX_FILES; i++) {
  71.         handles[i]=0;
  72.     }
  73. }
  74.  
  75. int fileno(FILE* f)
  76. {
  77.     int i ;
  78.     if (f) {
  79.         for (i=0; i < MAX_FILES; i++) {
  80.             if (handles[i] == f) {
  81.                 return i;
  82.             }
  83.          }
  84.     }
  85.     return -1 ;
  86. }
  87.  
  88. int unlink(const char* name)
  89. {
  90.     return !DeleteFile((STRPTR) name);
  91. }
  92.  
  93. FILE * fdopen(int fh, const char*)
  94. {
  95.     if (handles[fh])
  96.         return handles[fh] ;
  97.     return 0;
  98. }
  99.